﻿using MQ.Common;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MQ.Producer
{
    public class 字节阵MQ任务推送
    {
        public static void Go()
        {
            //创建连接工厂
            using var conn = RabbitMQHelper.GetConnection();
            //创建信道
            using var channel = conn.CreateModel();
            //创建交换机
            channel.ExchangeDeclare("ex_fanout_km", ExchangeType.Fanout);//交换机名,交换机类型
                                                                         //创建3个队列                                                             
            string queueName1 = "queue_fanout_name1";
            string queueName2 = "queue_fanout_name2";
            string queueName3 = "queue_fanout_name3";
            channel.QueueDeclare(queueName1, false, false, false, null);
            channel.QueueDeclare(queueName2, false, false, false, null);
            channel.QueueDeclare(queueName3, false, false, false, null);
            //将队列绑定到交换机
            channel.QueueBind(queue: queueName1, exchange: "ex_fanout_km", routingKey: "");//队列名，交换机名，路由键
            channel.QueueBind(queue: queueName2, exchange: "ex_fanout_km", routingKey: "");//队列名，交换机名，路由键
            channel.QueueBind(queue: queueName3, exchange: "ex_fanout_km", routingKey: "");//队列名，交换机名，路由键

            var value = string.Empty;
            do
            {
                Console.WriteLine("请输入消息内容：");
                value = Console.ReadLine();
                string msg = $" Fanout 扇形交换机 消息： ‘{value}’ ";
                var body = Encoding.UTF8.GetBytes(msg);
                Console.WriteLine($" 消息 ‘{value}’发送成功！");
                channel.BasicPublish("ex_fanout_km", "", null, body);
            } while (value != "out");
        }
    }
}
